package com.hyh.admin.sys.controller.download;

import com.hyh.ad.common.utils.zip.ZipFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;

@RestController
@RequestMapping("/api")
@Api(tags = "下载文件")
public class DownloadController {

    @Autowired
    private ZipFileService zipFileService;

    /*
     * 下载成zip文件
     */
    @GetMapping("/download")
    @ApiOperation("下载文件")
    public ResponseEntity<byte[]> downloadFiles(@RequestParam String[] fileNames) {
        // TODO: 下载文件 未完善
        try {
            System.out.println("fileNames: " + Arrays.toString(fileNames));
            ByteArrayOutputStream baos = zipFileService.createZipFile(fileNames);
            byte[] zipBytes = baos.toByteArray();

            HttpHeaders headers = new HttpHeaders();
            headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"files.zip\"");
            headers.set(HttpHeaders.CONTENT_TYPE, "application/zip");
            return new ResponseEntity<>(zipBytes, headers, HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace(); // 输出异常信息
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
